﻿CREATE FUNCTION [dbo].[fs_StatesParamValue](	
	@Param sysname
)RETURNS TABLE
RETURN
SELECT     ISNULL(S_P.StateParameterValue, S_P_P.StateParameterValue) AS StateParameterValue, ISNULL(S.ID, S_P_P.StateID) AS StateID
FROM         t_State_Parameters AS S_P WITH (NOLOCK) RIGHT OUTER JOIN
                      t_State AS S ON S_P.StateID = S.ID AND S_P.StateParameterID = @Param LEFT OUTER JOIN
                      t_State_Parameters AS S_P_P ON S.ParentID = S_P_P.StateID AND S_P_P.StateParameterID = @Param
WHERE ISNULL(S_P.StateID, S_P_P.StateID)>0


